package cn.itliuliu.org.controller;

import cn.itliuliu.basic.util.AjaxResult;
import cn.itliuliu.basic.util.PageList;
import cn.itliuliu.org.domain.Sdytype;
import cn.itliuliu.org.query.SdytypeQuery;
import cn.itliuliu.org.service.ISdytypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RequestMapping("/sdy")
@RestController
@Validated
public class SdytypeController {
    @Autowired
    private ISdytypeService sdytypeService;

    @GetMapping("/{id}")
    public Sdytype queryOne(@PathVariable("id") Long id) {
        return sdytypeService.queryOne(id);
    }

    @GetMapping
    public List<Sdytype> queryAll() {
        return sdytypeService.queryAll();
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody @Valid Sdytype sdytype) {
        if (sdytype.getId() != null) {
            sdytypeService.update(sdytype);
        } else {
            sdytypeService.add(sdytype);
        }
        return AjaxResult.me();
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        sdytypeService.delete(id);
        return AjaxResult.me();
    }

    @PostMapping
    public PageList<Sdytype> queryPage(@RequestBody SdytypeQuery sdytypeQuery) {
        return sdytypeService.queryPage(sdytypeQuery);
    }

    /**
     * 这里既可以用数组接收也可以用List接收
     * 用什么类型来接收对sql有影响
     *
     * @return
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        sdytypeService.patchDelete(ids);
        return AjaxResult.me();
    }

    @GetMapping("/tree")
    public List<Sdytype> queryTree() {
        return sdytypeService.queryTree();
    }
}
